package com.gdhuarui.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Slf4j
@Aspect
@Component
public class HttpAspect {

    @Pointcut("execution(* com.gdhuarui.controller..*.*(..))")
    public void pointCut() {
    }

    @Around(value = "pointCut()")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

        // 获取目标参数
        String serviceUniqueName = proceedingJoinPoint.getSignature().getDeclaringTypeName();
        String methodName = proceedingJoinPoint.getSignature().getName();
        Object[] args = proceedingJoinPoint.getArgs();

        // 记录方法调用信息
        log.info("@Http: {}.{} 被调用，参数: {}", serviceUniqueName, methodName, Arrays.toString(args));

        long start = System.currentTimeMillis();
        Object proceed = proceedingJoinPoint.proceed();

        // 记录方法调用耗时
        log.info("@Http: {}.{} 调用完成，耗时: {}ms", serviceUniqueName, methodName, System.currentTimeMillis() - start);

        return proceed;
    }
}